import Vue from "vue";
import Vuex from "vuex";
import { CLEAR } from "@/utils/constant/mutation-types.js";
Vue.use(Vuex);

const files = require.context("./modules", false, /\.js$/);
const modules = files.keys().reduce((module, current) => {
  const key = current.match(/\w+/)[0]; //匹配出文件名：'app' 'user'
  module[key] = files(current).default;
  return module;
}, {});

const store = new Vuex.Store({
  modules: modules,
  mutations: {
    [CLEAR](state) {
      state.user.mobile = "";
      state.user.Yhdtoken = "";
      state.user.role = "";
      state.user.recommendCode = "";
      state.user.userId = "";
      state.app.send = false;
    }
  },
  actions: {
    /* 退出登录 */
    logout({ commit }) {
      window.localStorage.removeItem("app");
      window.sessionStorage.clear();
      commit(CLEAR);
    }
  }
});

export default store;
